package com.qf.service.impl;

import com.qf.mapper.StuInfoMapper;
import com.qf.mapper.impl.StuInfoMapperImpl;
import com.qf.pojo.Page;
import com.qf.pojo.StuInfo;
import com.qf.service.ShowService;
import com.qf.until.MD5Utils;

import java.util.List;
import java.util.Random;

/**
 * @author 沐辰
 * @version v1.0
 * @description:
 * @project _4.30_Exam_two
 * @Date 2024/4/30 10:58
 * @company qf
 */
public class ShowServiceImpl implements ShowService {

    private StuInfoMapper stuInfoMapper = new StuInfoMapperImpl();
    /**
     * 管理端添加学生信息
     * @param stuInfo
     * @return
     */
    @Override
    public boolean insertMsg(StuInfo stuInfo) {
        if (stuInfo == null) {
            return false;
        }
        stuInfo.setPwd(MD5Utils.md5(stuInfo.getPwd()).substring(0,20));
        int insert = stuInfoMapper.insert(stuInfo);
        if (insert==1) {
            return true;
        }
        return false;
    }

    /**
     * 分页查询
     * @param page
     * @return
     */
    @Override
    public Page<StuInfo> showMsg(int page) {
        Page<StuInfo> p = new Page<>(page,2);
        //计算总条数
        long count = stuInfoMapper.count();
        p.setCountSize((int) count);
        //存入数据
        List<StuInfo> stuInfos = stuInfoMapper.listMsg(p.getOffset(), p.getSize());
        p.setPageMsg(stuInfos);
        return p;
    }

    /**
     * 管理端删除功能实现
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(int id) {
        int delete = stuInfoMapper.delete(id);
        if (delete == 1) {
            return true;
        }
        return false;
    }

    /**
     * 用户端更改个人信息
     * @param id
     * @return
     */
    @Override
    public StuInfo selectById(int id) {
        StuInfo stuInfo = stuInfoMapper.selectById(id);
        if (stuInfo != null) {
            return stuInfo;
        }
        return null;
    }

    /**
     * 根据id修改信息
     * @param stuInfo
     * @return
     */
    @Override
    public boolean updateById(StuInfo stuInfo) {
        String pwd = stuInfo.getPwd();
        stuInfo.setPwd(MD5Utils.md5(pwd).substring(0,20));
        int i = stuInfoMapper.updateById(stuInfo);
        if (i==1) {
            return true;
        }
        return false;
    }
}
